<?php include "classes.inc" ?>
<?php include "constants.inc" ?>
<?php session_start(); ?>
<?php
	if ( isset($_SESSION['user']) == false || $_SESSION['user']->club_id != 100 ) {
    	redirect("/links/index.php");
    }
	$user = $_SESSION['user'];
    
    $connection = mysql_connect(DB_SERVER, DB_USER, DB_PWD);
	mysql_select_db(DB_CATALOG);
    
    $result = mysql_query("select linkCategoryId, categoryName from link_category order by linkCategoryId", $connection);
    $linkCategoryItems = array();
    while ($row = mysql_fetch_assoc($result)) {
        if (!isset($linkCategoryId) || $linkCategoryId != $row['linkCategoryId']) {
            $linkCategoryId = $row['linkCategoryId'];
            $linkCategoryItem = new LinkCategoryItem;
            $linkCategoryItem->link_category_id = $linkCategoryId;
            $linkCategoryItem->link_category = $row['categoryName'];
            array_push($linkCategoryItems, $linkCategoryItem);
        }
    }
    
    $result = mysql_query("select club_id, clubname from club where clubname <> '' order by club_id", $connection);
    $clubItems = array();
    while ($row = mysql_fetch_assoc($result)) {
        if (!isset($clubId) || $clubId != $row['club_id']) {
            $clubId = $row['club_id'];
            $clubItem = new ClubItem;
            $clubItem->club_id = $clubId;
            $clubItem->clubname = $row['clubname'];
            array_push($clubItems, $clubItem);
        }
    }
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Invoer Link</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="../../assets/css/default.css" rel="stylesheet" type="text/css">
</head>

<body bottomMargin="0" leftMargin="0" topMargin="0" rightMargin="0" class="default">
<table border="0" cellpadding="0" cellspacing="0" width="100%">
    <tr>
        <td>
            <table border="0" cellpadding="0" cellspacing="0">
                <tr>
                    <td width="7">
                        <img src="/assets/images/spacer.gif" height="1" width="7" ALT="">
                    </td>
                    <td width="150" align="left"><img src="/assets/images/headerbar_kerst.gif" alt="" ></td>

                </tr>
                <tr>
                    <td colspan="2" height="12"><img src="/assets/images/spacer.gif" alt="" height="12" width="1"></td>
                </tr>
            </table>
        </td>
        <td align="right" valign="top">&nbsp;
		<br/>
       <?php
    	    if ( isset($_SESSION['user']) ) {
	            $user = $_SESSION['user'];
	    ?>
                    <a href="/logout.php?redirect=/links/index.php"><img src="/assets/images/24x24/plain/exit.gif" border="0" alt="Log uit"></a>
					<br>&nbsp;<br>
					<span class="headerSmall">User <?=$_SESSION['user']->name?> logged in at <?=$user->login_date?>.</span>
	    <?php
	        }
	    ?>
        <td>&nbsp;</td>
		<td>&nbsp;</td>
	</tr>
</table>

<table border="0" cellpadding="0" cellspacing="0" width="100%">
    <tr>
        <td align="left">            
            <table border="0" cellpadding="0" cellspacing="0">
                <tr>
                    <td rowspan="2" width="7"><img src="../assets/images/spacer.gif" alt="" height="1" width="7"></td>
					
					<td valign="top"><img src="../assets/images/tabcorners/re_99CCFF_l.gif" height="14" width="14" ALT=""></td>
					<td nowrap rowspan="2" class="inactiveSpacer"><img src="../assets/images/spacer.gif" alt="" height="3" width="3"><br/><a href="/newsbox.php" target="_top"><span class="tabName">Nieuwsbox</span></a></td>
					<td valign="top"><img src="../assets/images/tabcorners/re_99CCFF_r.gif" height="14" width="14" ALT=""></td>
					<td rowspan="2" width=2><img src="../assets/images/spacer.gif" alt="" height="1" width="2"></td>
					
					<td valign="top"><img src="../assets/images/tabcorners/re_99CCFF_l.gif" height="14" width="14" ALT=""></td>
                    <td nowrap rowspan="2" class="inactiveSpacer"><img src="../assets/images/spacer.gif" alt="" height="3" width="3"><br/><a href="/tornooien/tornooien.html" target="_top"><span class="tabName">Tornooien</span></a></td>
                    <td valign="top"><img src="../assets/images/tabcorners/re_99CCFF_r.gif" height="14" width="14" ALT=""></td>
                    <td rowspan="2" width=2><img src="../assets/images/spacer.gif" alt="" height="1" width="2"></td>
					
					<td valign="top"><img src="../assets/images/tabcorners/re_99CCFF_l.gif" height="14" width="14" ALT=""></td>
                    <td nowrap rowspan="2" class="inactiveSpacer"><img src="../assets/images/spacer.gif" alt="" height="3" width="3"><br/><a href="/bestuur/bestuur.php" target="_top"><span class="tabName">Bestuur</span></a></td>
                    <td valign="top"><img src="../assets/images/tabcorners/re_99CCFF_r.gif" height="14" width="14" ALT=""></td>
                    <td rowspan="2" width=2><img src="../assets/images/spacer.gif" alt="" height="1" width="2"></td>					
					
					<td valign="top"><img src="../assets/images/tabcorners/re_99CCFF_l.gif" height="14" width="14" ALT=""></td>
                    <td nowrap rowspan="2" class="inactiveSpacer"><img src="../assets/images/spacer.gif" alt="" height="3" width="3"><br/><a href="/advalvas/index.html" target="_top"><span class="tabName">Ad Valvas</span></a></td>
                    <td valign="top"><img src="../assets/images/tabcorners/re_99CCFF_r.gif" height="14" width="14" ALT=""></td>
                    <td rowspan="2" width=2><img src="../assets/images/spacer.gif" alt="" height="1" width="2"></td>
					
					<td valign="top"><img src="../assets/images/tabcorners/re_99CCFF_l.gif" height="14" width="14" ALT=""></td>
                    <td nowrap rowspan="2" class="inactiveSpacer"><img src="../assets/images/spacer.gif" alt="" height="3" width="3"><br/><a href="http://www.frbe-kbsb.be/EloHist/FRBE_Fiche.php" target="_blank"><span class="tabName">ELO</span></a></td>
                    <td valign="top"><img src="../assets/images/tabcorners/re_99CCFF_r.gif" height="14" width="14" ALT=""></td>
                    <td rowspan="2" width=2><img src="../assets/images/spacer.gif" alt="" height="1" width="2"></td>					
					
					<td valign="top"><img src="../assets/images/tabcorners/re_99CCFF_l.gif" height="14" width="14" ALT=""></td>
                    <td nowrap rowspan="2" class="inactiveSpacer"><img src="../assets/images/spacer.gif" alt="" height="3" width="3"><br/><a href="/clubs/clubs.php" target="_top"><span class="tabName">Clubs</span></a></td>
                    <td valign="top"><img src="../assets/images/tabcorners/re_99CCFF_r.gif" height="14" width="14" ALT=""></td>
                    <td rowspan="2" width=2><img src="../assets/images/spacer.gif" alt="" height="1" width="2"></td>
					
					<td valign="top"><img src="../assets/images/tabcorners/re_99CCFF_l.gif" height="14" width="14" ALT=""></td>
                    <td nowrap rowspan="2" class="inactiveSpacer"><img src="../assets/images/spacer.gif" alt="" height="3" width="3"><br/><a href="/jeugd/index.html" target="_top"><span class="tabName">Jeugd</span></a></td>
                    <td valign="top"><img src="../assets/images/tabcorners/re_99CCFF_r.gif" height="14" width="14" ALT=""></td>
                    <td rowspan="2" width=2><img src="../assets/images/spacer.gif" alt="" height="1" width="2"></td>
					
					<td valign="top"><img src="../assets/images/tabcorners/re_FFFFFF_l.gif" height="14" width="14" ALT=""></td>
                    <td nowrap rowspan="2" class="activeSpacer"><img src="../assets/images/spacer.gif" alt="" height="3" width="3"><br/><a href="/links/index.php" target="_top"><span class="tabName">Links</span></a></td>
                    <td valign="top"><img src="../assets/images/tabcorners/re_FFFFFF_r.gif" height="14" width="14" ALT=""></td>
                    <td rowspan="2" width=2><img src="../assets/images/spacer.gif" alt="" height="1" width="2"></td>
				</tr>
				<tr>
					<td class="inactiveSpacer"><img src="../assets/images/spacer.gif" alt=""></td>				
					<td class="inactiveSpacer"><img src="../assets/images/spacer.gif" alt=""></td>
					<td class="inactiveSpacer"><img src="../assets/images/spacer.gif" alt=""></td>
					<td class="inactiveSpacer"><img src="../assets/images/spacer.gif" alt=""></td>									
					<td class="inactiveSpacer"><img src="../assets/images/spacer.gif" alt=""></td>
					<td class="inactiveSpacer"><img src="../assets/images/spacer.gif" alt=""></td>					
					<td class="inactiveSpacer"><img src="../assets/images/spacer.gif" alt=""></td>
					<td class="inactiveSpacer"><img src="../assets/images/spacer.gif" alt=""></td>
					<td class="inactiveSpacer"><img src="../assets/images/spacer.gif" alt=""></td>
					<td class="inactiveSpacer"><img src="../assets/images/spacer.gif" alt=""></td>
					<td class="inactiveSpacer"><img src="../assets/images/spacer.gif" alt=""></td>
					<td class="inactiveSpacer"><img src="../assets/images/spacer.gif" alt=""></td>
					<td class="inactiveSpacer"><img src="../assets/images/spacer.gif" alt=""></td>
					<td class="inactiveSpacer"><img src="../assets/images/spacer.gif" alt=""></td>
					<td class="activeSpacer"><img src="../assets/images/spacer.gif" alt=""></td>
					<td class="activeSpacer"><img src="../assets/images/spacer.gif" alt=""></td>
				</tr>				
			</table>
		</td>
	</tr>
	<tr>
		<td width="100%" bgcolor="#FFFFFF" colspan="13"><img border="0" width="30" src="../assets/images/FFFFFF.gif" height="12" alt=""></td>
	</tr>
</table>

<?php
    $success = false;
    if ( isset($_POST['linkName']) ) {
    	$user = $_SESSION['user'];
		$linkCategoryId = $_POST['linkCategory'];
        $clubId = $_POST['club'];
        $linkName = $_POST['linkName'];
        $linkUrl = $_POST['linkUrl'];
		$linkNote = $_POST['linkNote'];
        
        if ( !empty($clubId)) {
            $linkCategoryId = 2; // fixed value ! repair later
        }

		if ( empty($linkCategoryId) ) {
        	echo "<center><strong style='color:#FFFFFF'>Niet alle velden zijn ingevuld.<strong><center>";
        } else {
        	$result = mysql_query("SET AUTOCOMMIT=0", $connection);
	        $result = mysql_query("BEGIN", $connection);

            $query = sprintf("INSERT INTO link (linkCategoryId, clubId, linkName, linkUrl, linkNote)
				VALUES ('%u', '%u', '%s', '%s', '%s')",
			$linkCategoryId, $clubId, mysql_real_escape_string($linkName),
			mysql_real_escape_string($linkUrl), mysql_real_escape_string($linkNote));

            if ($result = mysql_query($query, $connection)) {
				if (mysql_affected_rows($connection) == 1) {
                	$result = mysql_query("COMMIT", $connection);
                    $success = true;
                } else {
                	$result = mysql_query("ROLLBACK", $connection);
                	show_error("Link kon niet worden opgeslagen (".mysql_error().")");
                }
            } else {
                show_error("Link kon niet worden opgeslagen (".mysql_error().")");
            }
        }
    }
?>
<table height="100%" cellSpacing="0" cellPadding="0" width="100%" border="0" bgcolor="#FFFFFF">
<tr>
		<td id="leftFiller" valign="top" height="100%" rowspan="3" bgcolor="#666666">
			<table border="0" cellpadding="0" cellspacing="0" height="100%" width="8px">
				<tr><td>&nbsp;</td></tr>
			</table>
		</td>
		<td id="header" valign="top" width="100%">
			<table border="0" width="100%" cellpadding="3" cellspacing="0" bgcolor="#10425b">
				<tr>
					<td bgcolor="#666666" width="70%">
						<span class="headerLarge">Links</span>
						<br>
						<span class="headerMedium">Algemeen, blogs, clubs, organisaties</span>
					</td>
					<td bgcolor="#666666" align="right">
					<?php
						if (isset($user) && $user->club_id=100) {
					?>
						<a href="insert_link_form.php"><img src="/assets/images/16x16/add.gif" border="0" alt="Voeg nieuwsbericht toe"></a>
						&nbsp;&nbsp;
                    <?php
                    	}
					?>
				</tr>
			</table>
			<table align="center" border="0" width="100%" height="90%" cellpadding="0" cellspacing="0" style="border-top:#BCDD10 4px solid;">
				<tr>
					<td>&nbsp;</td>
					<td width="100%" height="20" colspan="2" class="quicklink">
						<a href="index.php">overzicht</a> |
                        <a href="alegmeen.php">algemeen</a> |
						<a href="blogs.php">blogs</a> |
                        <a href="clubs.php">clubs</a> |
                        <a href="organisaties.php">organisaties</a>
					</td>
				</tr>
				<tr>
					<td colspan="2" nowrap bgcolor="#BCDD10" height="4"><img src="../assets/images/spacer.gif" alt="" height="1" width="1"></td>
				</tr>
				<tr>
					<td>&nbsp;</td>
					<td valign="top">
						<table align="left" cellspacing="10" width="100%">
							<tr>
								<td>
								<span class="blackHeaderLarge">Invoer link</span>
								<br>
							</tr>
							<tr>
								<td>
									<form action="" method="post">
                                    <table cellspacing="5" class="yellowBox">
                                    	<tr>
                                    		<td width="30%"><strong>Categorie</strong></td>
                                    		<td>
                                                <select name="linkCategory">
                                                <?php
                                                    foreach ($linkCategoryItems as $item) {
                                                ?>
                                                    <option value="<?=$item->link_category_id?>"><?=$item->link_category?>
                                                <?php
                                                    }
                                                ?>
                                                </select>
                                            </td>
                                    	</tr>
                                        <tr>
                                    		<td width="30%"><strong>Club</strong></td>
                                    		<td>
                                                <select name="club">
                                                    <option value="" />Geen club
                                                <?php
                                                    foreach ($clubItems as $item) {
                                                ?>
                                                    <option value="<?=$item->club_id?>"><?=$item->club_id.
                                                        "&nbsp;&nbsp;&nbsp;&nbsp;".$item->clubname?>
                                                <?php
                                                    }
                                                ?>
                                                </select>
                                            </td>
                                    	</tr>
                                        <tr>
                                    		<td><strong>naam</strong></td>
                                    		<td><input name="linkName" type="text" size="30" /></td>
                                    	</tr>
                                        <tr>
                                    		<td><strong>URL</strong></td>
                                    		<td><input name="linkUrl" type="text" size="50" value="http://" /></td>
                                    	</tr>
										<tr>
                                    		<td><strong>Info</strong></td>
                                    		<td><textarea name="linkNote" cols="80" rows="5"></textarea></td>
                                    	</tr>
                                        <tr>
											<td colspan="2"><input type="submit" value="Voeg nieuwe link toe" /></td>
										</tr>
	                                </table>
	                                </form>
								</td>
							</tr>
                            <tr>
								<td><a href="index.php"><img border="0" src="../../assets/images/24x24/shadow/home.gif"></a></td>
							</tr>
							<?php
                            	if ($success) {
                            ?>
                            <tr>
								<td><strong style="background-color:#FFFFCC">Link is opgeslagen.</strong></td>
							</tr>
                            <?php
                                }
							?>
						</table>
					</td>
				</tr>
			</table>
		</td>
	</tr>
</table>

</body>
</html>
